package com.ruoyi.project.ledger.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.WeightStationDangerMapper;
import com.ruoyi.project.ledger.domain.WeightStationDanger;
import com.ruoyi.project.ledger.service.IWeightStationDangerService;

/**
 * 隐患自查自纠Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-04-23
 */
@Service
public class WeightStationDangerServiceImpl implements IWeightStationDangerService 
{
    @Autowired
    private WeightStationDangerMapper weightStationDangerMapper;

    /**
     * 查询隐患自查自纠
     * 
     * @param id 隐患自查自纠ID
     * @return 隐患自查自纠
     */
    @Override
    public WeightStationDanger selectWeightStationDangerById(Long id)
    {
        return weightStationDangerMapper.selectWeightStationDangerById(id);
    }

    /**
     * 查询隐患自查自纠列表
     * 
     * @param weightStationDanger 隐患自查自纠
     * @return 隐患自查自纠
     */
    @Override
    public List<WeightStationDanger> selectWeightStationDangerList(WeightStationDanger weightStationDanger)
    {
        if (StringUtils.isNotBlank(weightStationDanger.getUploadTime())){
            weightStationDanger.setUploadTime(weightStationDanger.getUploadTime()+"-01-01");
        }
        return weightStationDangerMapper.selectWeightStationDangerList(weightStationDanger);
    }

    /**
     * 新增隐患自查自纠
     * 
     * @param weightStationDanger 隐患自查自纠
     * @return 结果
     */
    @Override
    public int insertWeightStationDanger(WeightStationDanger weightStationDanger)
    {
        weightStationDanger.setCreateTime(DateUtils.getNowDate());
        weightStationDanger.setCreateId(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return weightStationDangerMapper.insertWeightStationDanger(weightStationDanger);
    }

    /**
     * 修改隐患自查自纠
     * 
     * @param weightStationDanger 隐患自查自纠
     * @return 结果
     */
    @Override
    public int updateWeightStationDanger(WeightStationDanger weightStationDanger)
    {
        return weightStationDangerMapper.updateWeightStationDanger(weightStationDanger);
    }

    /**
     * 批量删除隐患自查自纠
     * 
     * @param ids 需要删除的隐患自查自纠ID
     * @return 结果
     */
    @Override
    public int deleteWeightStationDangerByIds(Long[] ids)
    {
        for (Long id : ids){
            WeightStationDanger danger = weightStationDangerMapper.selectWeightStationDangerById(id);
            if (StringUtils.isNotBlank(danger.getFilePath())){
                FileUtils.deleteFile(RuoYiConfig.getProfile()+ "/"+danger.getFilePath().substring(Constants.RESOURCE_PREFIX.length()+1,danger.getFilePath().length()));
            }
        }
        return weightStationDangerMapper.deleteWeightStationDangerByIds(ids);
    }

    /**
     * 删除隐患自查自纠信息
     * 
     * @param id 隐患自查自纠ID
     * @return 结果
     */
    @Override
    public int deleteWeightStationDangerById(Long id)
    {
        return weightStationDangerMapper.deleteWeightStationDangerById(id);
    }
}
